package com.yc.bigdata.flink.demo;

import com.google.common.collect.Lists;
import com.yc.bigdata.flink.demo.vo.UserAction;

import org.apache.flink.api.java.typeutils.ListTypeInfo;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;
import java.util.List;

/**
 * <p></p>
 * FlatMap: 一行变任意行(0~多行)
 * out.collect
 *
 * @author: YuanChilde
 * @date: 2020-02-15 13:57
 * @version: 1.0
 * Modification History:
 * Date    Author      Version     Description
 * -----------------------------------------------------------------
 * 2020-02-15 13:57    YuanChilde     1.0        新增
 */
public class DataStreamMapOperatorList {
    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 输入: 用户行为。某个用户在某个时刻点击或浏览了某个商品，以及商品的价格。
        DataStreamSource<UserAction> source = env.fromCollection(Arrays.asList(
                new UserAction("userID1", 1293984000, "click", "productID1", 10),
                new UserAction("userID2", 1293984001, "browse", "productID2", 8),
                new UserAction("userID1", 1293984002, "click", "productID1", 10)
        ));

        // 返回List需使用returns
        SingleOutputStreamOperator<List<UserAction>> result = source
                .map(urlInfo -> {
                    List<UserAction> list = Lists.newArrayList();
                    int newPrice = urlInfo.getProductPrice() * 8;
                    urlInfo.setProductPrice(newPrice);
                    list.add(urlInfo);
                    return list;
                }).returns(new ListTypeInfo(UserAction.class));

        result.print();

        env.execute();
    }
}
